package com.yanqu.road.server.cmd.monopolymarket;

import com.yanqu.road.entity.monopolymarket.office.MonopolyMarketOfficeTip;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.monopolymarket.MonopolyMarketProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.monopolymarket.MonopolyMarketModule;
import com.yanqu.road.server.manger.monopolymarket.MonopolyMarketMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;

/**
 * // 为什么需要校验
 * 因为有可能 位置变动跨服没同步到区服
 *
 * @Description
 * @Author cwq
 * @Data 2021/8/20 10:25
 */
@Cmd(code = GameProtocol.S_MONOPOLY_MARKET_GET_USER, desc = "榷场获取玩家数据结果  跨服返回区服 协议")
public class MonopolyMarketGetUserFromCrossCmd extends AbstractCommand {
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        MonopolyMarketProto.MonopolyMarketCheckUserDataToGameServerMsg msg = MonopolyMarketProto.MonopolyMarketCheckUserDataToGameServerMsg.parseFrom(packet.getBytes());
        MonopolyMarketModule marketModule = player.getModule(MonopolyMarketModule.class);
        if(marketModule.baseData == null){
            return;
        }
        if(msg.hasRobedRecord()){
            // 先结算到原来被抢的时间
            marketModule.baseData.setOfficeCurPos(msg.getRobedRecord().getPosIndex());
            marketModule.baseData.setOfficeLevel(msg.getRobedRecord().getLevel());
            marketModule.baseData.setOfficeNo(msg.getRobedRecord().getNo());
            MonopolyMarketMgr.refreshOfficeTeaReward(marketModule,msg.getRobedRecord().getTime());
            MonopolyMarketMgr.refreshPhysical(marketModule, msg.getRobedRecord().getTime(),true);
            if(marketModule.inSystemId >= 0){
                MonopolyMarketProto.BroadcastMonopolyMarketRespMsg.Builder builder  = MonopolyMarketProto.BroadcastMonopolyMarketRespMsg.newBuilder();
                builder.setType(1);
                MonopolyMarketProto.MonopolyMarketOfficeTipEntity.Builder tipBuilder = MonopolyMarketProto.MonopolyMarketOfficeTipEntity.newBuilder();
                tipBuilder.setRobName(msg.getRobedRecord().getName());
                tipBuilder.setLevel(msg.getRobedRecord().getLevel());
                tipBuilder.setNo(msg.getRobedRecord().getNo());
                builder.setOfficeTip(tipBuilder);
                player.sendPacket(YanQuMessageUtils.buildMessage(ClientProtocol.U_MONOPOLY_MARKET_BROADCAST, builder));
            }else {
                MonopolyMarketOfficeTip officeTip = new MonopolyMarketOfficeTip();
                officeTip.robName = msg.getRobedRecord().getName();
                officeTip.level = msg.getRobedRecord().getLevel();
                officeTip.no = msg.getRobedRecord().getNo();
                marketModule.baseData.setOfficeTip(officeTip);
                marketModule.baseData.setHasTip(true);
            }
        }else {
            // 检查配置 分组是否发生变化
            marketModule.checkUserData();
        }

        // 周重置玩家
        if(!MonopolyMarketMgr.resetUser(marketModule)){
            // 同步跨服下发下来的数据
            marketModule.baseData.setOfficeCurPos(msg.getOfficePosIndex());
            marketModule.baseData.setOfficeLevel(msg.getOfficePosLevel());
            marketModule.baseData.setOfficeNo(msg.getOfficePosNo());
            marketModule.baseData.setOfficeSeatTime(msg.getOfficeSeatTime());
            // 同步派遣信息
            marketModule.getCurDispatchInfoListMap().clear();
            for (MonopolyMarketProto.MonopolyMarketSynDispatchInfoEntity item : msg.getDispatchInfoListList()){
                marketModule.addDispatchInfo(item.getType(),item.getPosIndex(),item.getDispatchObjectIdList(),item.getStartTime());
            }
            marketModule.synFortressDispatchInfo(msg.getFortressDispatchListList());
        }

        marketModule.baseData.setHasTip(msg.getHasTip());

        MonopolyMarketMgr.refreshUser(player,false,true);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
